/***
 * File  	: Snowflake.java
 * Subject	: Entry point to drawing a koch snowflake using android opengl es
 * Author 	: Isaac Parenteau
 * Date		: March 30th, 2010
 */

package src.project.koch;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ConfigurationInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class Snowflake extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener{
	
	private SnowflakeView _view;
	private SharedPreferences preferences;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Sets the view to full screen
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        Editor editor = preferences.edit();
        editor.putBoolean("rotateX", false);
        editor.putBoolean("rotateY", false);
        editor.putBoolean("rotateZ", false);
        editor.commit();
        preferences.registerOnSharedPreferenceChangeListener(this);
        
        _view = new SnowflakeView(this);
        if(detectOpenGLES20()){
        	_view.setEGLContextClientVersion(2);
        	Log.v("OpenGL ES", "Android device set to OpenGL ES 2.0");
        }
        else
        	Log.v("OpenGL ES", "Android device does not support OpenGL ES 2.0");
        
        setContentView(_view);
    }
    
    private boolean detectOpenGLES20() {
        ActivityManager am =
            (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo info = am.getDeviceConfigurationInfo();
        return (info.reqGlEsVersion >= 0x20000);
	}

	//Creates the menu on screen
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflator = getMenuInflater();
    	inflator.inflate(R.menu.menu, menu);
    	return true;
    }
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch(item.getItemId()){
    	case R.id.Preferences:
    		Intent i = new Intent(this, Preferences.class);
    		startActivity(i);
    		break;
    	case R.id.Again: //Generates a new view
    		_view.GetRenderer().Restart();
    		//_view = new SnowflakeView(this);
    		//setContentView(_view);
    		break;
    	case R.id.Exit:
    		finish();
    		break;
    	}
    	return true;
    }

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key) {
		
		Log.v("Key", key);
		
		String val = "";
		if(key.equals("iteration")){
			val = sharedPrefs.getString(key, "5");
			int iter = Integer.parseInt(val) < 5 ? Integer.parseInt(val) : 5;
			_view.GetRenderer().ChangeMaximumIterations(iter);
		}
		else if(key.equals("delay")){
			val = sharedPrefs.getString(key, "500");
			int delay = Integer.parseInt(val);
			_view.GetRenderer().ChangeDelay(delay);
		}
		else if(key.equals("rotateX") || key.equals("rotateY") || key.equals("rotateZ")){
			boolean rotateX = sharedPrefs.getBoolean("rotateX", false);
			boolean rotateY = sharedPrefs.getBoolean("rotateY", false);
			boolean rotateZ = sharedPrefs.getBoolean("rotateZ", false);
			_view.GetRenderer().Rotate(rotateX, rotateY, rotateZ);
		}
		else if(bgColorChange(key))
			ChangeBG(sharedPrefs);
		else if(ColorChange(key))
			ChangeColor(sharedPrefs);
		
		_view.GetRenderer().Restart();
	}

	/**
	 * Checks to see if the user modified the triangles color
	 * @param key
	 * @return
	 */
	private boolean ColorChange(String key) {
		if(key.equals("red") || key.equals("green") ||
				key.equals("blue") || key.equals("alpha"))
			return true;
		return false;
	}
	
	/**
	 * Checks to see if the user modified the background color
	 * @param key
	 * @return
	 */
	private boolean bgColorChange(String key) {
		
		if(key.equals("bgRed") || key.equals("bgGreen") ||
				key.equals("bgBlue") || key.equals("bgAlpha"))
			return true;
		
		return false;
	}

	/**
	 * Changes the color of the triangle
	 * @param sharedPrefs
	 */
	private void ChangeColor(SharedPreferences sharedPrefs) {
		String red = sharedPrefs.getString("red", "1.0");
		String green = sharedPrefs.getString("green", "1.0");
		String blue = sharedPrefs.getString("blue", "1.0");
		String alpha = sharedPrefs.getString("alpha", "1.0");
		
		float redVal = Float.parseFloat(red) < 1.0f ? Float.parseFloat(red) : 1.0f;
		float greenVal = Float.parseFloat(green) < 1.0f ? Float.parseFloat(green) : 1.0f;
		float blueVal = Float.parseFloat(blue) < 1.0f ? Float.parseFloat(blue) : 1.0f ;
		float alphaVal = Float.parseFloat(alpha) < 1.0f ? Float.parseFloat(alpha) : 1.0f;
		
		_view.GetRenderer().ChangeTriangleColor(redVal, greenVal, blueVal, alphaVal);
	}

	/**
	 * Changes the views background color
	 * @param sharedPrefs
	 */
	private void ChangeBG(SharedPreferences sharedPrefs) {
		
		String red = sharedPrefs.getString("bgRed", "0");
		String green = sharedPrefs.getString("bgGreen", "0");
		String blue = sharedPrefs.getString("bgBlue", "0");
		String alpha = sharedPrefs.getString("bgAlpha", "0");
		
		float redVal = Float.parseFloat(red) < 1.0f ? Float.parseFloat(red) : 1.0f;
		float greenVal = Float.parseFloat(green) < 1.0f ? Float.parseFloat(green) : 1.0f;
		float blueVal = Float.parseFloat(blue) < 1.0f ? Float.parseFloat(blue) : 1.0f ;
		float alphaVal = Float.parseFloat(alpha) < 1.0f ? Float.parseFloat(alpha) : 1.0f;
		
		_view.GetRenderer().ChangeBackgroundColor(redVal, greenVal, blueVal, alphaVal);
	}
}